package com.supreme.simpleserver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.supreme.commonmybatis.mapper.TestMapper;
import com.supreme.commonentity.mysql.primary.MBTest;
import com.supreme.simpleserver.annotation.SimpleFunction;
import com.supreme.simpleserver.annotation.SimpleTransactional;
import com.supreme.simpleserver.service.mybatis.MBTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class TestService {

    private final TestMapper testMapper;
    private final MBTestService mbTestService;

    @Autowired
    public TestService(TestMapper testMapper, MBTestService mbTestService) {
        this.testMapper = testMapper;
        this.mbTestService = mbTestService;
    }

    public List<MBTest> testList(String firstName) {
        QueryWrapper<MBTest> testQueryWrapper = new QueryWrapper<MBTest>()
                .eq("first_name", firstName);
        return testMapper.selectList(testQueryWrapper);
    }

    @SimpleFunction
    public List<MBTest> svcSearch() {
        log.info("svcSearch");
        return this.mbTestService.list();
    }

    @Transactional
    public int createTest(MBTest test, int state) {
        int result = this.testMapper.insert(test);
        int calcResult = 1 / state;
        log.info("createTest result: {} calcResult: {}", result, calcResult);
        return result;
    }

    @SimpleTransactional
    public int createTest2(MBTest test, int state) {
        boolean save = this.mbTestService.save(test);
        int result = save ? 1 : 0;
        int calcResult = 1 / state;
        log.info("createTest2 result: {} calcResult: {}", result, calcResult);
        return result;
    }
}
